<?php echo $this->render('/public/header'); ?>
<?php use yii\helpers\Url; ?>
<?php use yii; ?>
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <div id="main" class="col-xs-12">
        <a href="<?php echo Url::to(['setting/staff_add']); ?>" class="layui-btn layui-btn-normal">添加员工</a>
        <a href="<?php echo Url::to(['setting/download_staff']); ?>" class="layui-btn layui-btn-danger">下载excel</a>
        <br/><br/>
        <ul class="nav nav-tabs">
            <li onclick="change(0,this)" role="presentation"
                class="<?php echo yii::$app->request->get('status', 0) == 0 ? 'active' : ''; ?>"
                style="width:100px;text-align:center">在职
            </li>
            <li class="<?php echo yii::$app->request->get('status', 0) == 1 ? 'active' : ''; ?>"
                onclick="change(1,this)" role="presentation" style="width:100px;text-align:center">离职
            </li>
        </ul>
        <table class="table table-borderd">
            <thead>
            <tr>
                <th>工号</th>
                <th>部门</th>
                <th>姓名</th>
                <th>英文名</th>
                <th>入职时间
                    <div style="display: inline-block">
                        <i onclick="sortby('date','ASC')" class="fa fa-angle-double-up" aria-hidden="true"></i>
                        <i onclick="sortby('date','DESC')" class="fa fa-angle-double-down" aria-hidden="true"></i>
                    </div>
                </th>
                <th>手机号</th>
                <th style="width:200px">外部邮箱</th>
                <th style="width:200px">公司邮箱</th>
                <th style="width:200px">农商工资卡</th>
                <th>对应服务器号码</th>
                <th>是否离职</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php if ($list = $this->params['list']) { ?>
                <?php foreach ($list as $v) { ?>
                    <tr>
                        <td><?php echo str_pad($v['job_number'], 3, 0, STR_PAD_LEFT); ?></td>
                        <td><?php echo $v['group_name']; ?></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'name',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['name']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'en_name',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['en_name']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'date',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['date']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'phone',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['phone']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'email',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['email']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'company_email',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['company_email']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'bank_num',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['bank_num']; ?>"/></td>
                        <td><input onchange="edit_staff(<?php echo $v['id']; ?>,'server_num',this)"
                                   style="width:100%;border:none;padding:0;margin:0" type="text"
                                   value="<?php echo $v['server_num']; ?>"/></td>
                        <td><?php echo $v['status'] ? "离职" : "在职"; ?></td>
                        <td style="width:100px">
                            <!-- <a href="javascript:void(0)" onclick="real_del_staff(<?php echo $v['id']; ?>)" class="layui-btn layui-btn-mini layui-btn-normal">删除</a> -->
                            <a href="javascript:void(0);" onclick="del_staff(<?php echo $v['id']; ?>)"
                               class="layui-btn layui-btn-mini layui-btn-danger">离职</a>
                        </td>
                    </tr>
                <?php } ?>
            <?php } ?>
            </tbody>
        </table>
    </div>
    <script>
        var Status = {
            status:<?php echo yii::$app->request->get('status', 0);?>,
            sort: '<?php echo yii::$app->request->get('sort', 'DESC');?>'
        };

        function change(id, e) {
            $(e).siblings().removeClass('active');
            $(e).addClass('active');
            Status.status = id;
            const url = buildUrl();
            console.log(url);
            window.location.href = "/setting/staff?" + url;
        }

        function sortby(name, a) {
            Status.sort = a;
            const url = buildUrl();
            window.location.href = "/setting/staff?" + url;


        }

        function buildUrl() {
            let url = [];
            for (let i in Status) {
                let tmp = i + '=' + Status[i];
                url.push(tmp);
            }
            return url.join('&');
        }

        function real_del_staff(id) {
            if (layer.confirm("确认删除", {}, function () {
                    var url = "/setting/staff_real_del";
                    $.post(url, {id: id}, function (res) {
                        if (res.code == 1) {
                            layer.confirm(res.msg, {}, function () {
                                window.location.reload();
                            })
                        }
                        else {
                            layer.alert(res.msg);
                        }
                    }, 'json')
                })) ;
        }

        function del_staff(id) {
            if (layer.confirm('确认将此员工做离职操作', {}, function () {
                    var url = "<?php echo Url::to(['setting/staff_del']);?>";
                    $.post(url, {id: id}, function (res) {
                        if (res.code == 1) {
                            layer.confirm(res.msg, {}, function () {
                                window.location.reload();
                            })
                        }
                        else {
                            layer.alert(res.msg);
                        }
                    }, 'json')
                })) ;
        }

        function edit_staff(id, column, e) {
            if (layer.confirm('确认修改', {}, function () {
                    var data = {id: id, column: column, v: e.value};
                    var url = "<?php echo Url::to(['setting/staff_edit_by_column']);?>";
                    $.post(url, data, function (res) {
                        layer.alert(res.msg);
                    }, 'json')
                })) ;
        }
    </script>
<?php echo $this->render('/public/footer'); ?>